/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.henryschein.mmvvcc.controller;

/**
 *
 * @author obelog
 */
import com.henryschein.mmvvcc.dao.PersonDao;
import com.henryschein.mmvvcc.model.Person;
import com.henryschein.mmvvcc.parser.PaserXML;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.logging.Level;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private PersonDao personDao;
    private static final Logger logger = LoggerFactory.getLogger(AdminController.class);
    
    @RequestMapping(method = RequestMethod.GET)
	public String login(ModelMap model) {
	return "login";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/task")
    public String printWelcome(ModelMap model) {
        model.addAttribute("message", "Spring Security Hello Admin");
        return "admin";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/import")
    public ModelAndView importQuote(ModelMap model) {
        Person person = new Person();
        ModelAndView mav = new ModelAndView();
        mav.setViewName("import");
        mav.addObject("person", person);
        return mav;
    }

    @RequestMapping(method = RequestMethod.POST, value = "/import")
    public ModelAndView importQuotePost(@ModelAttribute Person person) {
        ModelAndView mav = new ModelAndView();
        mav.setViewName("import");
        String file = person.getQuote();
        for (Person p : new PaserXML().parse(file)){
            personDao.save(p);
        }
        mav.addObject("message", "File: " + file + " loaded successful");
        return mav;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/list")
    public ModelAndView listPeople() {
        logger.debug("Received request to list persons");
        ModelAndView mav = new ModelAndView();
        List<Person> people = personDao.getPeople();
        logger.debug("Person Listing count = " + people.size());
        mav.addObject("people", people);
        mav.setViewName("list2");
        return mav;
    }

    @RequestMapping(method = RequestMethod.GET, value = "/expor")
    public void exporPeople(HttpServletResponse response) {
        response.setContentType("application/xml");
        List<Person> people = personDao.getPeople();
        StringBuilder sb = new StringBuilder("<?xml version=\"1.0\"?><quotes>");
        for (Person p : people) {
            sb.append("\n <quote>");
            sb.append("<cont>" + p.getQuote() + "</cont>");
            sb.append("<cat>" + p.getCategory() + "</cat>");
            sb.append("<vote>" + p.getVote() + "</vote>");
            sb.append("</quote>");
        }
        sb.append("</quotes>");
        try {
            PrintWriter pr = response.getWriter();
            pr.write(sb.toString());
        } catch (IOException ex) {
            java.util.logging.Logger.getLogger(AdminController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @RequestMapping(method = RequestMethod.POST, value = "/edit")
    public String savePerson(@ModelAttribute Person person) {
        logger.debug("Received postback on person " + person);
        personDao.save(person);
        return "redirect:list";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/remove")
    public String removePerson(@RequestParam(value = "id", required = false) Long id) {
        logger.info("Remove person " + id);
        Person person = personDao.find(id);
        personDao.remove(person);
        return "redirect:list";
    }

    @RequestMapping(method = RequestMethod.GET, value = "/edit")
    public ModelAndView editPerson(@RequestParam(value = "id", required = false) Long id) {
        logger.debug("Received request to edit person id : " + id);
        ModelAndView mav = new ModelAndView();
        mav.setViewName("edit");
        Person person = null;
        if (id == null) {
            person = new Person();
        } else {
            person = personDao.find(id);
        }

        mav.addObject("person", person);
        return mav;
    }
}
